package com.xy.generate.generator;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.ArrayUtil;
import lombok.extern.slf4j.Slf4j;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;

@Slf4j
public class StaticFileGenerator {
    /**
     * @param inputPath  in
     * @param outputPath ou
     * @description: 拷贝目录及其文件
     * @author tzf
     * @date: 2024/6/13 20:34
     */

    public static void copyFilesByHutool(String inputPath, String outputPath) {
        FileUtil.copy(inputPath, outputPath, true);
    }

    public static void copyFilesByRecursive(String inputPath, String outputPath) {
        File inputFile = new File(inputPath);
        File outputFile = new File(outputPath);
        try {
            copyFilesByRecursive(inputFile, outputFile);
        } catch (Exception e) {
            log.info("复制文件失败!{}=>{}", inputPath, outputPath);
        }
    }

    private static void copyFilesByRecursive(File inputFile, File outputFile) throws IOException {
        if (inputFile.isDirectory()) {
            File destOutPutFile = new File(outputFile, inputFile.getName());
            if (!destOutPutFile.exists()) {
                destOutPutFile.mkdirs();
            }
            File[] files = inputFile.listFiles();
            if (ArrayUtil.isEmpty(files)) {
                return;
            }
            for (File file : files) {
                copyFilesByRecursive(file, destOutPutFile);
            }
        }
        // 将inputFile文件复制到outputFile所在目录下，并保持inputFile的文件名不变，如果目标文件已存在，则覆盖原有文件。
        Path resolve = outputFile.toPath().resolve(inputFile.getName());
        Files.copy(inputFile.toPath(), resolve, StandardCopyOption.REPLACE_EXISTING);
    }
}
